package com.opentd.graphics;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;

import com.opentd.game.Game;

public class Block implements Drawable{
	
	public int x, y, id;
	public String imgPath;
	public boolean clipped, buildable;
	public boolean isSpawnBlock = false;
	public boolean isFinishBlock = false;
	
	/**
	 * Creates and returns a new default grass block
	 * 
	 * @param x - x position
	 * @param y - y position
	 * @return - A new default grass Block object
	 */
	public static final Block createGrassBlock(int x, int y){
		return new Block(x, y,  Game.Block.GRASS_BLOCK_ID, "dat/img/block_grass.png", true, true);
	}
	
	/**
	 * Creates and returns a new default path block
	 * 
	 * @param x - x position
	 * @param y - y position
	 * @return - A new default path Block object
	 */
	public static final Block createPathBlock(int x, int y){
		return new Block(x, y,  Game.Block.PATH_BLOCK_ID, "dat/img/block_path.png", false, false);
	}
	
	/**
	 * Creates and returns a new default sand block
	 * 
	 * @param x - x position
	 * @param y - y position
	 * @return - A new default stone Block object
	 */
	public static final Block createSandBlock(int x, int y){
		return new Block(x, y,  Game.Block.SAND_BLOCK_ID, "dat/img/block_sand.png", true, true);
	}
	
	/**
	 * Creates and returns a new default finish block
	 * 
	 * @param x - x position
	 * @param y - y position
	 * @return - A new default stone Block object
	 */
	public static final Block createFinishBlock(int x, int y){
		return new Block(x, y,  Game.Block.FINISH_BLOCK_ID, "dat/img/block_finish.png", false, false);
	}
	
	/**
	 * Constructor for a block with properties different from that of the standard blocks.
	 * 
	 * @param x - x location
	 * @param y - y location
	 * @param id - The blocks ID. Use the static constants Game.Block.BLOCKTYPE_BLOCK_ID
	 * @param imagePath - The path to the image sprite for the block
	 * @param clipped - Are enemies able to walk on this block?
	 * @param buildable - Are players able to build towers on this block.
	 */
	public Block(int x, int y, int id, String imagePath, boolean clipped, boolean buildable){
		this.x = x;
		this.y  = y;
		this.imgPath = imagePath;
		this.clipped = clipped;
		this.id = id;
		this.buildable = buildable;
	}
	
	@Override
	public void draw(Graphics g) {
		g.drawImage(new ImageIcon(imgPath).getImage() ,  (x * Game.Block.WIDTH), (y * Game.Block.HEIGHT), Game.Block.WIDTH, Game.Block.HEIGHT, null);
	}

}
